﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace Fares
{
    public partial class DocumenteRezumatFrm : FrmRezumat
    {
        private Boolean DocumenteIntrare = false;
        private BindingSource bsDoc = null;
        private BindingSource bsDocDetaliu = null;
        public Int32 idDocSel = -1;
        private Boolean AfiseazaCurent = false;
        private int LunaCurenta = 0;
        private int AnulCurent = 0;
        private Int32 IDArticolIntrate = -1;
        private Int32 IDArticolIesite = -1;
        private Decimal ValoareUnitaraArticol = -1;
        private Int16 FelDocument = -1;
        private Boolean GestiuneCantitativa = false;

        public DocumenteRezumatFrm(ModuleClass mI,Boolean dint,
            BindingSource ds,BindingSource dsd,
            Int32 _idSel,Boolean _afisCurenta,int _lunaCurenta,int _anCurent,
            Int32 idArticolIntrare,Int32 idArticolIesite,Decimal valoareunitara,Int16 felDoc,Boolean _GestiuneCantitativa):base (mI)
        {
            GestiuneCantitativa = _GestiuneCantitativa;
            FelDocument = felDoc;
            ValoareUnitaraArticol = valoareunitara;
            idDocSel = _idSel;
            DocumenteIntrare = dint;
            InitializeComponent();
            bsDoc = ds;
            bsDocDetaliu = dsd;
            ReadOnly = true;
            EnableMainBrowseButton = false;
            AfiseazaCurent = _afisCurenta;
            LunaCurenta = _lunaCurenta;
            AnulCurent = _anCurent;
            IDArticolIntrate = idArticolIntrare;
            IDArticolIesite = idArticolIesite;
            this.documenteDetaliuTableAdapter.SetTableName(ModuleInfo.AnArhiva);
            this.documenteTableAdapter.SetTableName(ModuleInfo.AnArhiva);
            if (ModuleInfo.AnArhiva != String.Empty)
                ReadOnly = true;

        }

        private void DocumenteRezumatFrm_Load(object sender, EventArgs e)
        {
            AddBindingSource(documenteBindingSource);
            AddBindingSource(documenteDetaliuBindingSource);
            //listTableAdaptor.Add(documenteTableAdapter.Adapter);

            try
            {
                SqlConnection con = DeschideConexiune(workConnString);
                articoleTableAdapter.Connection = con;
                articoleTableAdapter.Fill(eSRPDataSet.Articole, ModuleInfo.CodUnitate);
                this.regimFiscalTableAdapter.Connection = con;
                this.regimFiscalTableAdapter.Fill(this.eSRPDataSet.RegimFiscal, DocumenteIntrare ? "I" : "O");

                this.tipDocumentTableAdapter.Connection = con;
                this.tipDocumentTableAdapter.Fill(this.eSRPDataSet.TipDocument);
                this.parteneriTableAdapter.Connection = con;
                this.parteneriTableAdapter.Fill(this.eSRPDataSet.Parteneri);

                this.documenteDetaliuTableAdapter.Connection = con;
                this.documenteDetaliuTableAdapter.Fill(this.eSRPDataSet.DocumenteDetaliu);
                this.documenteTableAdapter.Connection = con;

                if (IDArticolIesite == -1 && IDArticolIntrate == -1)
                {
                    if (AfiseazaCurent)
                    {
                        if (FelDocument == -1)
                            this.documenteTableAdapter.FillByLunaCurenta(this.eSRPDataSet.Documente, ModuleInfo.CodUnitate, (decimal)LunaCurenta, (decimal)AnulCurent, DocumenteIntrare ? "I" : "O");
                        else
                            this.documenteTableAdapter.FillByLunaCurentaFel(this.eSRPDataSet.Documente, ModuleInfo.CodUnitate, (decimal)LunaCurenta, (decimal)AnulCurent, FelDocument, DocumenteIntrare ? "I" : "O");
                    }
                    else
                    {
                        if(FelDocument==-1)
                            this.documenteTableAdapter.Fill(this.eSRPDataSet.Documente, ModuleInfo.CodUnitate, DocumenteIntrare ? "I" : "O");
                        else
                            this.documenteTableAdapter.FillFel(this.eSRPDataSet.Documente, ModuleInfo.CodUnitate,FelDocument, DocumenteIntrare ? "I" : "O");

                    }
                }
                else
                {
                    eSRPDataSet.EnforceConstraints = false;

                    if (!GestiuneCantitativa)
                    {
                        if (IDArticolIntrate != -1)
                            this.documenteTableAdapter.FillByArticol(this.eSRPDataSet.Documente, ModuleInfo.CodUnitate, IDArticolIntrate, ValoareUnitaraArticol);
                        if (IDArticolIesite != -1)
                            this.documenteTableAdapter.FillByArticolIesire(this.eSRPDataSet.Documente, ModuleInfo.CodUnitate, IDArticolIesite, ValoareUnitaraArticol);
                    }
                    else
                    {
                        if (IDArticolIntrate != -1)
                            this.documenteTableAdapter.FillByArticolGC(this.eSRPDataSet.Documente, ModuleInfo.CodUnitate, IDArticolIntrate);
                        if (IDArticolIesite != -1)
                            this.documenteTableAdapter.FillByArticolIesireGC(this.eSRPDataSet.Documente, ModuleInfo.CodUnitate, IDArticolIesite);
                    }
                    //eSRPDataSet.EnforceConstraints = true;
                }


                InchideConexiune(con);
            }
            catch (Exception ex)
            {
                TrateazaExceptie(ex);
                return;
            }
            //documenteDGV.DataSource = bsDoc;
            //documenteDetaliuDGV.DataSource = bsDocDetaliu;
            documenteBindingSource.Sort = "[DataDoc] DESC";
            if (idDocSel != -1)
            {
                int pos = documenteBindingSource.Find("IDDocument", idDocSel);
                if (pos != -1)
                    documenteBindingSource.Position = pos;
            }
        }

        
    }
}
